package com.yanqu.road.server.gameplayer.module.activity.tomb.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.*;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.math.BigInteger;
import java.util.Date;

public class TombUserLogServiceCmd implements IGameServiceCommand {

    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        Tomb.TombLogCrossToGameReq resp = Tomb.TombLogCrossToGameReq.parseFrom(request.getDataBytes());

        if (resp.getLEnum() == Tomb.GameTombLogEnum.MOVE) {
            LogTombUserMove l = new LogTombUserMove();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            l.setFloor(resp.getFloor());
            l.setX(resp.getX());
            l.setY(resp.getY());
            l.setHp(resp.getHp());
            l.setEventId(resp.getMove().getEventId());
            AutoLogMgr.add(l);
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.BATTLE) {
            LogTombUserBattle l = new LogTombUserBattle();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            l.setFloor(resp.getFloor());
            l.setX(resp.getX());
            l.setY(resp.getY());
            l.setHp(resp.getHp());
            l.setEventId(resp.getMove().getEventId());
            l.setIsPvp(resp.getBattle().getIsPvp());
            l.setBattleId(resp.getBattle().getBattleId());
            l.setAttack(resp.getBattle().getAttack());
            l.setRivalMaxHp(resp.getBattle().getRivalMaxHp());
            l.setRivalHp(resp.getBattle().getRivalHp());
            l.setIsKill(resp.getBattle().getIsKill());
            l.setCostEnergy((int) resp.getBattle().getCostEnergy());
            AutoLogMgr.add(l);
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.ENERGY) {
            LogTombUserEnergy l = new LogTombUserEnergy();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            int energyChange = resp.getEnergy().getEnergyChange();
            l.setEnergyChange(resp.getEnergy().getEnergyChange());
            l.setEnergy(resp.getEnergy().getEnergy());
            l.setEventId(resp.getEnergy().getEventId());
            l.setDescription(resp.getEnergy().getDescription());
            AutoLogMgr.add(l);
            // 触发体力消耗事件
            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(resp.getUserId());
            if (onlinePlayer != null && energyChange < 0) {
                SeasonTaskConditionData data = new SeasonTaskConditionData(BigInteger.valueOf((long)TombMgr.getActivityInfo().getType()), BigInteger.valueOf((long)Math.abs(energyChange)));
                onlinePlayer.notifyListener(eGamePlayerEventType.TombCostStaminaTask.getValue(), data);
                return syncResponse;
            }
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.BUFF) {
            LogTombUserBuff l = new LogTombUserBuff();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            l.setFloor(resp.getFloor());
            l.setX(resp.getX());
            l.setY(resp.getY());
            l.setRelationId(resp.getBuff().getRelationId());
            l.setAbility((int) resp.getBuff().getAbility());
            l.setTotalAbility(resp.getBuff().getTotalAbility());
            AutoLogMgr.add(l);
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.CHOSE) {
            LogTombUserChose l = new LogTombUserChose();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            l.setFloor(resp.getFloor());
            l.setX(resp.getX());
            l.setY(resp.getY());
            l.setRelationId(resp.getChose().getRelationId());
            l.setIsRight(resp.getChose().getIsRight());
            l.setRewards(resp.getChose().getRewards());
            AutoLogMgr.add(l);
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.TRAP) {
            LogTombUserTrap l = new LogTombUserTrap();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            l.setFloor(resp.getFloor());
            l.setX(resp.getX());
            l.setY(resp.getY());
            l.setOutType(resp.getTrap().getOutType());
            AutoLogMgr.add(l);
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.SCORE) {
            LogTombUserScore l = new LogTombUserScore();
            l.setUserId(resp.getUserId());
            l.setActivityId(resp.getActivityId());
            l.setUnionUid(resp.getUnionUid());
            l.setType(resp.getScore().getType());
            l.setScoreChange(resp.getScore().getScoreChange());
            l.setScore(resp.getScore().getScore());
            AutoLogMgr.add(l);
        } else if (resp.getLEnum() == Tomb.GameTombLogEnum.GENERATE_BLOOD) {
            AutoLogMgr.add(new LogTombNpcBaseBlood(
                    resp.getActivityId(),
                    resp.getNpcBaseBlood().getNpcId(),
                    resp.getNpcBaseBlood().getHp1(),
                    resp.getNpcBaseBlood().getHp2(),
                    resp.getNpcBaseBlood().getMinHp(),
                    resp.getNpcBaseBlood().getResultHp()
            ));
        }



        return syncResponse;
    }
}
